package com.lebang.lechuangsystem.controller.sys;


import com.lebang.lechuangsystem.common.SessionUtil;
import com.lebang.lechuangsystem.domain.User;
import com.lebang.lechuangsystem.service.sys.UserService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.encrypt.MessageDigestUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录
 *
 * @Author: hwq
 * @Date: 2019/8/22 10:21
 * @Version 1.0
 */
@Api(tags = "登录")
@RestController
@RequestMapping("/login")
public class SysController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public JsonData test(HttpServletRequest request, String username, String password, HttpSession session) throws Exception {
        if (TextUtil.isEmpty(username, password)) {
            return Callback.fail("203", "参数非法");
        }
        User user = userService.selectByName(username);
        if (user == null) {
            return Callback.fail("201", "用户不存在");
        }

        if (!user.getPassword().equals(MessageDigestUtil.encryptMD5(password.getBytes()))) {
            return Callback.fail("202", "密码错误");
        }
        SessionUtil.setSession(user);
        user.setPassword("");
        return Callback.success("200", "登录成功!", user);
    }

    @PostMapping("/out")
    public JsonData out(HttpServletRequest request, String username, String password, HttpSession session) throws Exception {
        SessionUtil.removeSession();
        return Callback.success("200", "退出登录成功");
    }
}

